#include <Windows.h>
#include <gl/gl.h>
#include <assert.h>
#include "ShowListManager.h"

using namespace std;

ShowListManager::ShowListManager(void)
{
  ShowListInfo head = {1, ""};
  _showListContainer.push_back (head);
}

ShowListManager::~ShowListManager(void)
{
  glDeleteLists (1, _showListContainer.size () - 1);
}

void ShowListManager::createNewList (string name, GLenum mode)
{
  // Get the first free node in container
  unsigned int firstFree = _showListContainer[0].nextFree;
  if (firstFree == 0) // Cannot add any more show lists
    CTHROW (ShowListManagerError, ShowListManagerError::SHOW_LIST_FULL_ERROR);
  
  assert (!glIsList (firstFree));
  assert (firstFree <= _showListContainer.size ());
  
  ShowListInfo newShowListInfo = {0, name};

  if (firstFree == _showListContainer.size ()) // no free nodes in container
  {
    _showListContainer[0].nextFree = firstFree + 1;
    _showListContainer.push_back (newShowListInfo);
  }
  else
  {
    _showListContainer[0].nextFree = _showListContainer[firstFree].nextFree;
    _showListContainer[firstFree] = newShowListInfo;
  }

  glNewList (firstFree, mode);

}

unsigned int ShowListManager::getList (std::string name)
{
  for (size_t i = 0; i < _showListContainer.size (); ++i)
    if (name == _showListContainer[i].name)
      return i;
  CTHROW (ShowListManagerError, ShowListManagerError::SHOW_LIST_NOT_FOUND);
}

void ShowListManager::deleteList (string name)
{
  GLuint showListId = getList (name);
  if (showListId == 0) return;
  assert (glIsList (showListId));
  glDeleteLists (showListId, 1);

  _showListContainer[showListId].nextFree = _showListContainer[0].nextFree;
  _showListContainer[showListId].name = "";
  _showListContainer[0].nextFree = showListId;
}